﻿;(function($) {
        $.timer = function(func, time, autostart) {     
                this.set = function(func, time, autostart) {
                        this.init = true;
                        if(typeof func == 'object') {
                                var paramList = ['autostart', 'time'];
                                for(var arg in paramList) {if(func[paramList[arg]] != undefined) {eval(paramList[arg] + " = func[paramList[arg]]");}};
                                func = func.action;
                        }
                        if(typeof func == 'function') {this.action = func;}
                        if(!isNaN(time)) {this.intervalTime = time;}
                        if(autostart && !this.active) {
                                this.active = true;
                                this.setTimer();
                        }
                        return this;
                };
                this.once = function(time) {
                        var timer = this;
                        if(isNaN(time)) {time = 0;}
                        window.setTimeout(function() {timer.action();}, time);
                        return this;
                };
                this.play = function(reset) {
                        if(!this.active) {
                                if(reset) {this.setTimer();}
                                else {this.setTimer(this.remaining);}
                                this.active = true;
                        }
                        return this;
                };
                this.pause = function() {
                        if(this.active) {
                                this.active = false;
                                this.remaining -= new Date() - this.last;
                                this.clearTimer();
                        }
                        return this;
                };
                this.stop = function() {
                        this.active = false;
                        this.remaining = this.intervalTime;
                        this.clearTimer();
                        return this;
                };
                this.toggle = function(reset) {
                        if(this.active) {this.pause();}
                        else if(reset) {this.play(true);}
                        else {this.play();}
                        return this;
                };
                this.reset = function() {
                        this.active = false;
                        this.play(true);
                        return this;
                };
                this.clearTimer = function() {
                        window.clearTimeout(this.timeoutObject);
                };
                this.setTimer = function(time) {
                        var timer = this;
                        if(typeof this.action != 'function') {return;}
                        if(isNaN(time)) {time = this.intervalTime;}
                        this.remaining = time;
                        this.last = new Date();
                        this.clearTimer();
                        this.timeoutObject = window.setTimeout(function() {timer.go();}, time);
                };
                this.go = function() {
                        if(this.active) {
                                this.action();
                                this.setTimer();
                        }
                };
                
                if(this.init) {
                        return new $.timer(func, time, autostart);
                } else {
                        this.set(func, time, autostart);
                        return this;
                }
        };
})(jQuery);